package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.result.Result;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author liuying
 * @since 2022-09-22
 */
@Slf4j
@Api(tags = "后端 积分等级管理")
@CrossOrigin   //跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation(value = "积分等级列表", notes = "查看所有的积分等级列表")
    @GetMapping("/list")
    public Result getAll() {

        //测试日志输出
        log.info("hi this is log info");
        log.warn("hi this is log warn");
        log.error("hi this is log error");

        List<IntegralGrade> list = integralGradeService.list();
        return Result.ok().data("list",list).message("获取列表成功");
    }

    @ApiOperation(value = "删除信息", notes = "根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean b = integralGradeService.removeById(id);
        if (b){
            return Result.ok().message("删除成功");
        }else {
            return Result.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public Result save(@RequestBody IntegralGrade integralGrade){
//        if (integralGrade.getBorrowAmount() == null){
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_ERROR);
//        }
        //利用断言代替上面的if，抛异常
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_ERROR);
        boolean b = integralGradeService.save(integralGrade);
        if (b){
            return Result.ok().message("保存成功");
        }else {
            return Result.error().message("保存失败");
        }
    }


    @ApiOperation("根据id获取列表信息")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id){
        IntegralGrade byId = integralGradeService.getById(id);
        if (byId != null){
            return Result.ok().data("record",byId);
        }else {
            return Result.error().message("数据获取失败");
        }
    }

    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public Result updateById(@RequestBody IntegralGrade integralGrade){
        boolean b = integralGradeService.updateById(integralGrade);
        if (b){
            return Result.ok().message("更新成功");
        }else {
            return Result.error().message("更新失败");
        }
    }
}

